<template>
    <section>
        <!--高级查询列表-->
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
        <el-form :inline="true" :model="query">
            <el-form-item>
                <el-input v-model="query.keywords" placeholder="员工查找"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" v-on:click="search">查询</el-button>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="handleAdd">新增</el-button>
            </el-form-item>
        </el-form>
    </el-col>

        <!--列表-->
        <el-table :data="ListData" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55"></el-table-column>

            <el-table-column prop="username" label="用户名" width="120" sortable></el-table-column>
            <el-table-column prop="email" label="邮箱" width="100"  sortable></el-table-column>
            <el-table-column prop="phone" label="电话" width="100" sortable></el-table-column>
            <el-table-column prop="password" label="密码" width="120" sortable></el-table-column>
            <el-table-column prop="age" label="年龄" min-width="180" sortable></el-table-column>
            <el-table-column prop="state" label="状态" min-width="180" sortable :formatter="formatSex"></el-table-column>

            <el-table-column label="操作" width="150">
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="query.pageSize" :total="total" style="float:right;">
            </el-pagination>
        </el-col>


        <!--新增界面 ,表单addForm-->
        <el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="addForm.username" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="name">
                    <el-input v-model="addForm.email" auto-complete="off"></el-input>
                </el-form-item>

                <el-form-item label="电话" prop="name">
                    <el-input v-model="addForm.phone" auto-complete="off"></el-input>
                </el-form-item>

                <el-form-item label="密码" prop="name">
                    <el-input v-model="addForm.password" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="年龄">
                    <el-input-number v-model="addForm.age" :min="0" :max="200"></el-input-number>
                </el-form-item>
                <el-form-item label="状态">
                    <el-radio-group v-model="addForm.state">
                        <el-radio class="radio" :label="1">启用</el-radio>
                        <el-radio class="radio" :label="0">禁用</el-radio>
                    </el-radio-group>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <!--addFormVisible表示展开表单-->
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>



    </section>
</template>

<script>
    export default {

       data(){
           return{
               listLoading:false,
               ListData:[],
               sels:[],
               total:0,
               addFormVisible:false,
               addLoading:false,
               query:{
                   currentPage:1,
                   pageSize:7,
                   keywords:''
               },
               addForm:{
                   username:'',
                   email:'',
                   phone:null,
                   password:'',
                   age:0,
                   state:1
               },
               addFormRules: {
                   username: [
                       { required: true, message: '请输入用户名', trigger: 'blur' }
                   ]
               },

           }

       },
        methods:{
            // 批量删除
            batchRemove(){
                var ids = this.sels.map(item => item.id);
                this.$confirm('确认要离开我吗？， 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {

                    this.$http.patch("/emp",ids).then(res=>{

                        let{success,message}=res.data;
                        if(success){
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                        }else {
                            this.$message({
                                type: 'info',
                                message: '删除失败，操作异常'
                            });
                        }

                        this.loadList();
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });

            },

            // 当前页数据
            handleCurrentChange(val){
                this.query.currentPage=val;
                this.loadList()

            },

            formatSex: function (row, column) {
                return row.state == 1 ? '启用' : row.state == 0 ? '禁用' : '未知';
            },


           // 新增方法
            handleAdd(){
                this.addFormVisible=true;
                this.addForm={
                    username:'',
                    email:'',
                    phone:null,
                    password:'',
                    age:0,
                    state:1

                 }

            },
            // 添加方法
            addSubmit(){
                this.addFormVisible=true;
                this.$http.put("/emp",this.addForm).then(res=>{
                   let{success,message}=res.data;
                    if(success){
                        this.$message({
                            type: 'success',
                            message: '操作成功!'
                        });
                    }else {
                        this.$message({
                            type: 'info',
                            message: '操作失败，操作异常'
                        });
                    }
                    this.loadList();
                    this.addFormVisible=false;
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '您已取消删除'
                    });
                });

            },
            // 点击批量删除时为有效按钮
            selsChange(val){
                this.sels=val;


            },
            // 编辑方法
            handleEdit(index,row){
                this.addFormVisible=true;
                this.addForm = Object.assign({}, row);

            },
            // 删除方法
            handleDel(index,row){
                this.$confirm('确认要离开我吗？， 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    // 从row对象中将id取出来
                    let id = row.id;
                    // 拼接id请求路径
                    this.$http.delete("/emp/"+id).then(res=>{

                        let{success,message}=res.data;
                        if(success){
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                        }else {
                            this.$message({
                                type: 'info',
                                message: '删除失败，操作异常'
                            });
                        }

                        this.loadList();
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });

            },


            // 查询方法
            search(){
                this.query.currentPage=1;
                this.loadList();

            },
            // 展示数据
            loadList(){
                this.$http.post("/emp",this.query).then(res=>{
                    console.debug(res);
                    this.ListData=res.data.data;
                    this.total=res.data.total;

                })

            }

        },
        mounted(){

            this.loadList();

        }

    }

</script>

<style >

</style>